package com.cn.goodsadmin.service;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.po.GoodsInfo;
import com.cn.goodsadmin.po.OrderInfo;
import com.cn.goodsadmin.po.UserInfo;
import com.cn.goodsadmin.po.UserWordsInfo;
import com.cn.goodsadmin.util.TimeUtil;
import com.cn.goodsadmin.util.UrlUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service("userService")
public class UserService extends BaseService {

    public PageQueryResult<DestDTO> getAllUserInfo(int pageindex, int pagesize, int appid) throws Exception {
        return userDao.findAllUserInfo(pageindex, pagesize, appid);
    }

    public UserInfo updateUserInfo(String openid, int vip, int sore, int appid) throws Exception {
        UserInfo userInfo = userDao.checkUserByOpenid(openid, appid);
        if (userInfo != null) {
            userInfo.setcVid(vip);
            userInfo.setcSore(sore);
            userDao.update(userInfo);
        }
        return userInfo;
    }

    public UserInfo findUserInfoByOpenid(String openid,int appid)throws Exception{
        return userDao.checkUserByOpenid(openid,appid);
    }

    public HashMap<String, Long> analyNewAndOldUser(Date date, int appid) throws Exception {
        long newUser = userDao.findRegisterUserCountByDate(date, appid);
        long totalUser = actionRoutDao.getActionRountByDay(date, appid);
        long oldUser = totalUser - newUser;
        HashMap<String, Long> userAnaly = new HashMap<String, Long>();
        userAnaly.put("new", newUser);
        userAnaly.put("old", oldUser);
        userAnaly.put("total", totalUser);
        return userAnaly;
    }


    public HashMap<String, Long> getUserActivityLog(Date bdate, Date edate, int appid) throws Exception {
        List<UserInfo> userInfos = userDao.findRegisterUserByDate(bdate, edate, appid);
        HashMap<String, Long> map = new HashMap<String, Long>();
        for (UserInfo userInfo : userInfos) {
            String openid = userInfo.getcOpenid();
            long count = actionRoutDao.getUserActionRountByBeginTimeAndEndTime(bdate, TimeUtil.getCurrDay(0), openid, appid);
            map.put(openid, count);
        }
        return map;
    }

    public UserWordsInfo getUserInfoByOpenid(String openid, int appid) throws Exception {
        UserWordsInfo userWordsInfo = new UserWordsInfo();
        UserInfo userInfo = userDao.checkUserByOpenid(openid, appid);
        if (userInfo != null) {
            userInfo.cousumeCount = userBuyLogDao.findUserAllBuyCount(openid, appid);
            userWordsInfo.userInfo = userInfo;
            String reFav = userInfo.getcFav();
            String[] favs = UrlUtil.coverFav(reFav);
            List<GoodsInfo> reGoods=new ArrayList<GoodsInfo>();
            if(favs!=null) {
                for (String goodsid : favs) {
                    GoodsInfo goodsInfo = goodsInfoDao.findGoodsInfoByid(goodsid);
                    if (goodsInfo != null) {
                        reGoods.add(goodsInfo);
                    }
                }
            }
            userWordsInfo.recommendGoods=reGoods;//推荐商品
            List<OrderInfo> orderInfos=orderDao.findUserLastOrder(openid,appid,5);
            userWordsInfo.orderInfos=orderInfos;
        }
        return userWordsInfo;

    }

}
